package com.aier.admin_new.entity.mapper;

import java.sql.Connection;


import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Map;

import net.eicp.ghostboy.futil.StringUtil;

import org.springframework.jdbc.core.PreparedStatementCreator;
import org.springframework.jdbc.core.RowMapper;

import com.aier.admin_new.entity.Dynamic;

public class DynamicRowMapper implements RowMapper<Dynamic> {

    @SuppressWarnings("unused")
    private int type;

    public DynamicRowMapper() {
    }

    public DynamicRowMapper(int type) {
        this.type = type;
    }

    @Override
    public Dynamic mapRow(ResultSet rs, int rowNum) throws SQLException {
        return parseHome(new Dynamic(), rs);
    }

    public static Dynamic parseHome(Dynamic h, ResultSet rs) throws SQLException {
        return parseHome(h, rs, null);
    }

    public static Dynamic parseHome(Dynamic h, ResultSet rs, String su)
        throws SQLException {

        if (StringUtil.isStringNull(su)) {
            su = "";
        } else {
            su += ".";
        }
        try {
            h.setDid(rs.getLong(su+"did"));
        } catch (SQLException e) {
            h.setDid(null);
        }
        try {
            h.setUid(rs.getLong(su+"uid"));
        } catch (SQLException e) {
            h.setUid(null);
        }
        try {
            h.setSid(rs.getInt(su+"sid"));
        } catch (SQLException e) {
            h.setSid(null);
        }
        try {
            h.setCid(rs.getLong(su+"cid"));
        } catch (SQLException e) {
            h.setCid(null);
        }

        try {
            String str = rs.getString(su+"content");
            if (str != null) {
                str = str.replaceAll("<", "&lt;");
                str = str.replaceAll(">", "&gt;");
            }
           
            h.setContent(str);
        } catch (SQLException e) {
            h.setContent(null);
        }
        try {
            h.setImg(rs.getString(su+"img"));
        } catch (SQLException e) {
            h.setImg(null);
        }
        try {
            h.setType(rs.getShort(su+"type"));
        } catch (SQLException e) {
            h.setType(null);
        }
        try {
            h.setOriginal(rs.getShort(su+"original"));
        } catch (SQLException e) {
            h.setOriginal(null);
        }
        try {
            h.setRelativeid(rs.getLong(su+"relativeid"));
        } catch (SQLException e) {
            h.setRelativeid(null);
        }
        try {
            h.setPcount(rs.getInt(su+"pcount"));
        } catch (SQLException e) {
            h.setPcount(null);
        }
        try {
            h.setTcount(rs.getInt(su+"tcount"));
        } catch (SQLException e) {
            h.setTcount(null);
        }
        try {
            h.setCcount(rs.getInt(su+"ccount"));
        } catch (SQLException e) {
            h.setCcount(null);
        }
        try {
            h.setCdate(rs.getTimestamp(su+"cdate"));
        } catch (SQLException e) {
            h.setCdate(null);
        }
        try {
            h.setNickname(rs.getString(su+"nickname"));
        } catch (SQLException e) {
            h.setNickname(null);
        }
        try {
            h.setHeadimg(rs.getString(su+"headimg"));
        } catch (SQLException e) {
            h.setHeadimg(null);
        }
        return h;
    }

    public static Map<String, Object> getPar(Map<String, Object> par, Dynamic h) {
        par.put("did",h.getDid());
        par.put("uid",h.getUid());
        par.put("sid",h.getSid());
        par.put("cid",h.getCid());
        par.put("content",h.getContent());
        par.put("img",h.getImg());
        par.put("type",h.getType());
        par.put("original",h.getOriginal());
        par.put("relativeid",h.getRelativeid());
        par.put("pcount",h.getPcount());
        par.put("tcount",h.getTcount());
        par.put("ccount",h.getCcount());
        par.put("cdate",h.getCdate());
        par.put("nickname",h.getNickname());
        par.put("headimg",h.getHeadimg());
        return par;
    }

    public static Map<String, Object> getArgs(Map<String, Object> args, Dynamic h) {
        args.put("did",h.getDid() );
        return args;
    }

    /**
     * 
     * 
     * @param tfp
     * @return
     */
    public static PreparedStatementCreator getPreparedStatementCreator(
            final Dynamic h) {
        return new PreparedStatementCreator() {
            @Override
            public PreparedStatement createPreparedStatement(Connection con)
                throws SQLException {
                PreparedStatement ps = con.prepareStatement(parseSQL(h),
                        new String[] { "did" });
                parsePS(ps, h);
                return ps;
            }

        };
    }

    private static String parseSQL(Dynamic h) throws SQLException {
        StringBuffer sql = new StringBuffer("insert into dynamic(");
        int count = 0;
        if (null != h.getUid()) {
            sql.append("uid,");
            count++;
        }
        if (null != h.getSid()) {
            sql.append("sid,");
            count++;
        }
        if (null != h.getCid()) {
            sql.append("cid,");
            count++;
        }
        if (null != h.getContent()) {
            sql.append("content,");
            count++;
        }
        if (null != h.getImg()) {
            sql.append("img,");
            count++;
        }
        if (null != h.getType()) {
            sql.append("type,");
            count++;
        }
        if (null != h.getOriginal()) {
            sql.append("original,");
            count++;
        }
        if (null != h.getRelativeid()) {
            sql.append("relativeid,");
            count++;
        }
        if (null != h.getPcount()) {
            sql.append("pcount,");
            count++;
        }
        if (null != h.getTcount()) {
            sql.append("tcount,");
            count++;
        }
        if (null != h.getCcount()) {
            sql.append("ccount,");
            count++;
        }
        if (null != h.getCdate()) {
            sql.append("cdate,");
            count++;
        }
        
        sql = new StringBuffer(sql.substring(0, sql.length() - 1));
        sql.append(") value(");
        for (int i = 0; i < (count - 1); i++) {
            sql.append("?,");
        }
        return sql.append("?)").toString();
    }

    private static void parsePS(PreparedStatement ps, Dynamic h)
        throws SQLException {
        int count = 1;
        if (null != h.getUid()) {
            ps.setLong(count++,h.getUid());
        }
        if (null != h.getSid()) {
            ps.setInt(count++,h.getSid());
        }
        if (null != h.getCid()) {
            ps.setLong(count++,h.getCid());
        }
        
        if (null != h.getContent()) {
            ps.setString(count++,h.getContent() );
        }
        if (null != h.getImg()) {
            ps.setString(count++,h.getImg());
        }
        if (null != h.getType()) {
            ps.setShort(count++,h.getType());
        }
        if (null != h.getOriginal()) {
            ps.setShort(count++,h.getOriginal());
        }
        if (null != h.getRelativeid()) {
            ps.setLong(count++,h.getRelativeid());
        }
        if (null != h.getPcount()) {
            ps.setInt(count++,h.getPcount());
        }
        if (null != h.getTcount()) {
            ps.setInt(count++,h.getTcount());
        }
        if (null != h.getCcount()) {
            ps.setInt(count++,h.getCcount());
        }
        if (null != h.getCdate()) {
            ps.setTimestamp(count++,h.getCdate());
        }
    }

}
